<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>emailaddress_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_format_emailaddress_test.js.html">emailaddress_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.format.EmailAddressTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.format.EmailAddressTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.array&#39;);
<a name="line19"></a>goog.require(&#39;goog.format.EmailAddress&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line21"></a>
<a name="line22"></a>function testparseList() {
<a name="line23"></a>  assertParsedList(&#39;&#39;, [], &#39;Failed to parse empty stringy&#39;);
<a name="line24"></a>  assertParsedList(&#39;,,&#39;, [], &#39;Failed to parse string with commas only&#39;);
<a name="line25"></a>
<a name="line26"></a>  assertParsedList(&#39;&lt;foo@gmail.com&gt;&#39;, [&#39;foo@gmail.com&#39;]);
<a name="line27"></a>
<a name="line28"></a>  assertParsedList(&#39;&lt;foo@gmail.com&gt;,&#39;, [&#39;foo@gmail.com&#39;],
<a name="line29"></a>      &#39;Failed to parse 1 address with trailing comma&#39;);
<a name="line30"></a>
<a name="line31"></a>  assertParsedList(&#39;&lt;foo@gmail.com&gt;, &#39;, [&#39;foo@gmail.com&#39;],
<a name="line32"></a>      &#39;Failed to parse 1 address with trailing whitespace and comma&#39;);
<a name="line33"></a>
<a name="line34"></a>  assertParsedList(&#39;,&lt;foo@gmail.com&gt;&#39;, [&#39;foo@gmail.com&#39;],
<a name="line35"></a>      &#39;Failed to parse 1 address with leading comma&#39;);
<a name="line36"></a>
<a name="line37"></a>  assertParsedList(&#39; ,&lt;foo@gmail.com&gt;&#39;, [&#39;foo@gmail.com&#39;],
<a name="line38"></a>      &#39;Failed to parse 1 address with leading whitespace and comma&#39;);
<a name="line39"></a>
<a name="line40"></a>  assertParsedList(&#39;&lt;foo@gmail.com&gt;, &lt;bar@gmail.com&gt;&#39;,
<a name="line41"></a>      [&#39;foo@gmail.com&#39;, &#39;bar@gmail.com&#39;],
<a name="line42"></a>      &#39;Failed to parse 2 email addresses&#39;);
<a name="line43"></a>
<a name="line44"></a>  assertParsedList(&#39;&lt;foo@gmail.com&gt;, &lt;bar@gmail.com&gt;,&#39;,
<a name="line45"></a>      [&#39;foo@gmail.com&#39;, &#39;bar@gmail.com&#39;],
<a name="line46"></a>      &#39;Failed to parse 2 email addresses and trailing comma&#39;);
<a name="line47"></a>
<a name="line48"></a>  assertParsedList(&#39;&lt;foo@gmail.com&gt;, &lt;bar@gmail.com&gt;, &#39;,
<a name="line49"></a>      [&#39;foo@gmail.com&#39;, &#39;bar@gmail.com&#39;],
<a name="line50"></a>      &#39;Failed to parse 2 email addresses, trailing comma and whitespace&#39;);
<a name="line51"></a>
<a name="line52"></a>  assertParsedList(
<a name="line53"></a>      &#39;John Doe &lt;john@gmail.com&gt;; Jane Doe &lt;jane@gmail.com&gt;, &#39; +
<a name="line54"></a>          &#39;&lt;jerry@gmail.com&gt;&#39;,
<a name="line55"></a>      [&#39;john@gmail.com&#39;, &#39;jane@gmail.com&#39;, &#39;jerry@gmail.com&#39;],
<a name="line56"></a>      &#39;Failed to parse addresses with semicolon separator&#39;);
<a name="line57"></a>}
<a name="line58"></a>
<a name="line59"></a>function testparseListOpenersAndClosers() {
<a name="line60"></a>  assertParsedList(
<a name="line61"></a>      &#39;aaa@gmail.com, &quot;bbb@gmail.com&quot;, &lt;ccc@gmail.com&gt;, &#39; +
<a name="line62"></a>          &#39;(ddd@gmail.com), [eee@gmail.com]&#39;,
<a name="line63"></a>      [&#39;aaa@gmail.com&#39;, &#39;&quot;bbb@gmail.com&quot;&#39;, &#39;ccc@gmail.com&#39;,
<a name="line64"></a>        &#39;(ddd@gmail.com)&#39;, &#39;[eee@gmail.com]&#39;],
<a name="line65"></a>      &#39;Failed to handle all 5 opener/closer characters&#39;);
<a name="line66"></a>}
<a name="line67"></a>
<a name="line68"></a>function testparseListIdn() {
<a name="line69"></a>  var idnaddr = &#39;mailtest@\u4F8B\u3048.\u30C6\u30B9\u30C8&#39;;
<a name="line70"></a>  assertParsedList(idnaddr, [idnaddr]);
<a name="line71"></a>}
<a name="line72"></a>
<a name="line73"></a>function testparseListWithQuotedSpecialChars() {
<a name="line74"></a>  var res = assertParsedList(
<a name="line75"></a>      &#39;a\\&quot;b\\&quot;c &lt;d@e.f&gt;,&quot;g\\&quot;h\\&quot;i\\\\&quot; &lt;j@k.l&gt;&#39;,
<a name="line76"></a>      [&#39;d@e.f&#39;, &#39;j@k.l&#39;]);
<a name="line77"></a>  assertEquals(&#39;Wrong name 0&#39;, &#39;a&quot;b&quot;c&#39;, res[0].getName());
<a name="line78"></a>  assertEquals(&#39;Wrong name 1&#39;, &#39;g&quot;h&quot;i\\&#39;, res[1].getName());
<a name="line79"></a>}
<a name="line80"></a>
<a name="line81"></a>function testparseListWithCommaInLocalPart() {
<a name="line82"></a>  var res = assertParsedList(
<a name="line83"></a>      &#39;&quot;Doe, John&quot; &lt;doe.john@gmail.com&gt;, &lt;someone@gmail.com&gt;&#39;,
<a name="line84"></a>      [&#39;doe.john@gmail.com&#39;, &#39;someone@gmail.com&#39;]);
<a name="line85"></a>
<a name="line86"></a>  assertEquals(&#39;Doe, John&#39;, res[0].getName());
<a name="line87"></a>  assertEquals(&#39;&#39;, res[1].getName());
<a name="line88"></a>}
<a name="line89"></a>
<a name="line90"></a>function testparseListWithWhitespaceSeparatedEmails() {
<a name="line91"></a>  var res = assertParsedList(
<a name="line92"></a>      &#39;a@b.com &lt;c@d.com&gt; e@f.com &quot;G H&quot; &lt;g@h.com&gt; i@j.com&#39;,
<a name="line93"></a>      [&#39;a@b.com&#39;, &#39;c@d.com&#39;, &#39;e@f.com&#39;, &#39;g@h.com&#39;, &#39;i@j.com&#39;]);
<a name="line94"></a>  assertEquals(&#39;G H&#39;, res[3].getName());
<a name="line95"></a>}
<a name="line96"></a>
<a name="line97"></a>function testparseListSystemNewlines() {
<a name="line98"></a>  // These Windows newlines can be inserted in IE8, or copied-and-pasted from
<a name="line99"></a>  // bad data on a Mac, as seen in bug 11081852.
<a name="line100"></a>  assertParsedList(&#39;a@b.com\r\nc@d.com&#39;, [&#39;a@b.com&#39;, &#39;c@d.com&#39;],
<a name="line101"></a>      &#39;Failed to parse Windows newlines&#39;);
<a name="line102"></a>  assertParsedList(&#39;a@b.com\nc@d.com&#39;, [&#39;a@b.com&#39;, &#39;c@d.com&#39;],
<a name="line103"></a>      &#39;Failed to parse *nix newlines&#39;);
<a name="line104"></a>  assertParsedList(&#39;a@b.com\n\rc@d.com&#39;, [&#39;a@b.com&#39;, &#39;c@d.com&#39;],
<a name="line105"></a>      &#39;Failed to parse obsolete newlines&#39;);
<a name="line106"></a>  assertParsedList(&#39;a@b.com\rc@d.com&#39;, [&#39;a@b.com&#39;, &#39;c@d.com&#39;],
<a name="line107"></a>      &#39;Failed to parse pre-OS X Mac newlines&#39;);
<a name="line108"></a>}
<a name="line109"></a>
<a name="line110"></a>function testToString() {
<a name="line111"></a>  var f = function(str) {
<a name="line112"></a>    return goog.format.EmailAddress.parse(str).toString();
<a name="line113"></a>  };
<a name="line114"></a>
<a name="line115"></a>  // No modification.
<a name="line116"></a>  assertEquals(&#39;JOHN Doe &lt;john@gmail.com&gt;&#39;,
<a name="line117"></a>               f(&#39;JOHN Doe &lt;john@gmail.com&gt;&#39;));
<a name="line118"></a>
<a name="line119"></a>  // Extra spaces.
<a name="line120"></a>  assertEquals(&#39;JOHN Doe &lt;john@gmail.com&gt;&#39;,
<a name="line121"></a>               f(&#39; JOHN  Doe  &lt;john@gmail.com&gt; &#39;));
<a name="line122"></a>
<a name="line123"></a>  // No name.
<a name="line124"></a>  assertEquals(&#39;john@gmail.com&#39;, f(&#39;&lt;john@gmail.com&gt;&#39;));
<a name="line125"></a>  assertEquals(&#39;john@gmail.com&#39;, f(&#39;john@gmail.com&#39;));
<a name="line126"></a>
<a name="line127"></a>  // No address.
<a name="line128"></a>  assertEquals(&#39;JOHN Doe&#39;, f(&#39;JOHN Doe &lt;&gt;&#39;));
<a name="line129"></a>
<a name="line130"></a>  // Special char in the name.
<a name="line131"></a>  assertEquals(&#39;&quot;JOHN, Doe&quot; &lt;john@gmail.com&gt;&#39;,
<a name="line132"></a>               f(&#39;JOHN, Doe &lt;john@gmail.com&gt;&#39;));
<a name="line133"></a>
<a name="line134"></a>  // Already quoted.
<a name="line135"></a>  assertEquals(&#39;&quot;JOHN, Doe&quot; &lt;john@gmail.com&gt;&#39;,
<a name="line136"></a>               f(&#39;&quot;JOHN, Doe&quot; &lt;john@gmail.com&gt;&#39;));
<a name="line137"></a>
<a name="line138"></a>  // Needless quotes.
<a name="line139"></a>  assertEquals(&#39;JOHN Doe &lt;john@gmail.com&gt;&#39;,
<a name="line140"></a>               f(&#39;&quot;JOHN Doe&quot; &lt;john@gmail.com&gt;&#39;));
<a name="line141"></a>  // Not quoted-string, but has double quotes.
<a name="line142"></a>  assertEquals(&#39;&quot;JOHN, Doe&quot; &lt;john@gmail.com&gt;&#39;,
<a name="line143"></a>               f(&#39;JOHN, &quot;Doe&quot; &lt;john@gmail.com&gt;&#39;));
<a name="line144"></a>
<a name="line145"></a>  // No special characters other than quotes.
<a name="line146"></a>  assertEquals(&#39;JOHN Doe &lt;john@gmail.com&gt;&#39;,
<a name="line147"></a>               f(&#39;JOHN &quot;Doe&quot; &lt;john@gmail.com&gt;&#39;));
<a name="line148"></a>
<a name="line149"></a>  // Escaped quotes are also removed.
<a name="line150"></a>  assertEquals(&#39;&quot;JOHN, Doe&quot; &lt;john@gmail.com&gt;&#39;,
<a name="line151"></a>               f(&#39;JOHN, \\&quot;Doe\\&quot; &lt;john@gmail.com&gt;&#39;));
<a name="line152"></a>}
<a name="line153"></a>
<a name="line154"></a>function doIsValidTest(testFunc, valid, invalid) {
<a name="line155"></a>  goog.array.forEach(valid, function(str) {
<a name="line156"></a>    assertTrue(&#39;&quot;&#39; + str + &#39;&quot; should be valid.&#39;, testFunc(str));
<a name="line157"></a>  });
<a name="line158"></a>  goog.array.forEach(invalid, function(str) {
<a name="line159"></a>    assertFalse(&#39;&quot;&#39; + str + &#39;&quot; should be invalid.&#39;, testFunc(str));
<a name="line160"></a>  });
<a name="line161"></a>}
<a name="line162"></a>
<a name="line163"></a>function testIsValid() {
<a name="line164"></a>  var valid = [
<a name="line165"></a>    &#39;e@b.eu&#39;, &#39;&lt;a.b+foo@c.com&gt;&#39;, &#39;eric &lt;e@b.com&gt;&#39;, &#39;&quot;e&quot; &lt;e@b.com&gt;&#39;,
<a name="line166"></a>    &#39;a@FOO.MUSEUM&#39;, &#39;bla@b.co.ac.uk&#39;, &#39;bla@a.b.com&#39;, &#39;o\&#39;hara@gm.com&#39;,
<a name="line167"></a>    &#39;plus+is+allowed@gmail.com&#39;, &#39;!/#$%&amp;\&#39;*+-=~|`{}?^_@expample.com&#39;,
<a name="line168"></a>    &#39;confirm-bhk=modulo.org@yahoogroups.com&#39;];
<a name="line169"></a>  var invalid = [
<a name="line170"></a>    &#39;e&#39;, &#39;&#39;, &#39;e @c.com&#39;, &#39;a@b&#39;, &#39;foo.com&#39;, &#39;foo@c..com&#39;, &#39;test@gma=il.com&#39;,
<a name="line171"></a>    &#39;aaa@gmail&#39;, &#39;has some spaces@gmail.com&#39;, &#39;has@three@at@signs.com&#39;,
<a name="line172"></a>    &#39;@no-local-part.com&#39;];
<a name="line173"></a>  doIsValidTest(goog.format.EmailAddress.isValidAddress, valid, invalid);
<a name="line174"></a>}
<a name="line175"></a>
<a name="line176"></a>function testIsValidLocalPart() {
<a name="line177"></a>  var valid = [
<a name="line178"></a>    &#39;e&#39;, &#39;a.b+foo&#39;, &#39;o\&#39;hara&#39;, &#39;user+someone&#39;, &#39;!/#$%&amp;\&#39;*+-=~|`{}?^_&#39;,
<a name="line179"></a>    &#39;confirm-bhk=modulo.org&#39;];
<a name="line180"></a>  var invalid = [
<a name="line181"></a>    &#39;A@b@c&#39;, &#39;a&quot;b(c)d,e:f;g&lt;h&gt;i[j\\k]l&#39;, &#39;just&quot;not&quot;right&#39;,
<a name="line182"></a>    &#39;this is&quot;not\\allowed&#39;, &#39;this\\ still\&quot;not\\\\allowed&#39;, &#39;has some spaces&#39;];
<a name="line183"></a>  doIsValidTest(goog.format.EmailAddress.isValidLocalPartSpec, valid, invalid);
<a name="line184"></a>}
<a name="line185"></a>
<a name="line186"></a>function testIsValidDomainPart() {
<a name="line187"></a>  var valid = [
<a name="line188"></a>    &#39;example.com&#39;, &#39;dept.example.org&#39;, &#39;long.domain.with.lots.of.dots&#39;];
<a name="line189"></a>  var invalid = [&#39;&#39;, &#39;@has.an.at.sign&#39;, &#39;..has.leading.dots&#39;, &#39;gma=il.com&#39;,
<a name="line190"></a>    &#39;DoesNotHaveADot&#39;];
<a name="line191"></a>  doIsValidTest(goog.format.EmailAddress.isValidDomainPartSpec, valid, invalid);
<a name="line192"></a>}
<a name="line193"></a>
<a name="line194"></a>
<a name="line195"></a>/**
<a name="line196"></a> * Asserts that parsing the inputString produces a list of email addresses
<a name="line197"></a> * containing the specified address strings, irrespective of their order.
<a name="line198"></a> * @param {string} inputString A raw address list.
<a name="line199"></a> * @param {Array.&lt;string&gt;} expectedList The expected results.
<a name="line200"></a> * @param {string=} opt_message An assertion message.
<a name="line201"></a> * @return {string} the resulting email address objects.
<a name="line202"></a> */
<a name="line203"></a>function assertParsedList(inputString, expectedList, opt_message) {
<a name="line204"></a>  var message = opt_message || &#39;Should parse address correctly&#39;;
<a name="line205"></a>  var result = goog.format.EmailAddress.parseList(inputString);
<a name="line206"></a>  assertEquals(
<a name="line207"></a>      &#39;Should have correct # of addresses&#39;, expectedList.length, result.length);
<a name="line208"></a>  for (var i = 0; i &lt; expectedList.length; ++i) {
<a name="line209"></a>    assertEquals(message, expectedList[i], result[i].getAddress());
<a name="line210"></a>  }
<a name="line211"></a>  return result;
<a name="line212"></a>}
</pre>


</body>
</html>
